package com.meichis.yndms.ui.activity;

import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.view.View;
import android.widget.DatePicker;

import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.GridLayoutManager;

import com.meichis.mcsappframework.dialog.DropDownDialog;
import com.meichis.mcsappframework.utils.DateUtil;
import com.meichis.yndms.R;
import com.meichis.yndms.adapter.TallyPhotosAdapter;
import com.meichis.yndms.databinding.ActivityTallyPhotosBinding;
import com.meichis.yndms.model.Staff;
import com.meichis.yndms.model.TallyPhotosTypeSub;
import com.meichis.yndms.ui.common.BaseActivity;
import com.meichis.yndms.vm.TallyPhotosVM;

import java.util.ArrayList;
import java.util.Calendar;

public class TallyPhotosActivity extends BaseActivity {

    private ActivityTallyPhotosBinding binding;
    private TallyPhotosVM vm;
    private TallyPhotosAdapter adapter;

    @Override
    protected void setDataBindingContentView() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_tally_photos);
        vm = new ViewModelProvider(this, new ViewModelProvider.NewInstanceFactory()).get(TallyPhotosVM.class);
        binding.setVm(vm);
        binding.setClick(new ClickEvent());
        binding.setLifecycleOwner(this);
    }

    @Override
    protected void init() {
        binding.navigation.txtTitle.setText("业务理货照片");
        binding.navigation.navBack.setOnClickListener(v -> onBackPressed());
        vm.queryStaffList();
        vm.querySubTypeList();
        addObserver();
        initRecycleView();
    }

    private void addObserver() {
        vm.getShowDialog().observe(this, aBoolean -> showLoading(aBoolean));

        Staff staff = new Staff();
        staff.setRealName("请选择业务员");
        staff.setDMSID("");
        vm.getStaff().setValue(staff);

        vm.getSubList().observe(this, datas -> {
            if (datas == null || datas.size() < 1) {
                TallyPhotosTypeSub tallyPhotosTypeSub = new TallyPhotosTypeSub();
                tallyPhotosTypeSub.setPhotoTypeName("");
                tallyPhotosTypeSub.setID(0);
                vm.getSubType().setValue(tallyPhotosTypeSub);
                return;
            }

            vm.getSubType().setValue(vm.getSubList().getValue().get(0));
        });

        vm.getPhotoList().observe(this, datas -> {
                    adapter.setList(datas);
                    adapter.setType(vm.getType().getValue().getName(),
                            vm.getSubType().getValue().getPhotoTypeName());
                }
        );
    }

    private void initRecycleView() {
        adapter = new TallyPhotosAdapter(this, R.layout.item_tally_photos, new ArrayList<>());
        binding.rc.setLayoutManager(new GridLayoutManager(this, 2));
        binding.rc.setHasFixedSize(true);
        binding.rc.setItemAnimator(null);
        binding.rc.setAdapter(adapter);
    }

    public class ClickEvent {

        public void selectSalesMan() {
            new DropDownDialog<>(TallyPhotosActivity.this, R.color.theme, "业务员", "RealName", vm.getStaffList().getValue(), new DropDownDialog.SelectedListener() {
                @Override
                public void setSelect(Object o, int position) {
                    vm.getStaff().setValue(vm.getStaffList().getValue().get(position));
                }
            }).show();
        }

        public void selectTallyTime() {
            final Calendar cd = Calendar.getInstance();
            new DatePickerDialog(TallyPhotosActivity.this, AlertDialog.THEME_HOLO_LIGHT, new DatePickerDialog.OnDateSetListener() {
                public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                    cd.set(Calendar.YEAR, year);
                    cd.set(Calendar.MONTH, monthOfYear);
                    cd.set(Calendar.DAY_OF_MONTH, dayOfMonth);
                    vm.getTallyDate().setValue(DateUtil.date2Str(cd, DateUtil.FORMAT_YMD));
                }
            }, cd.get(Calendar.YEAR), cd.get(Calendar.MONTH), cd.get(Calendar.DAY_OF_MONTH)).show();
        }

        public void selectType() {
            new DropDownDialog<>(TallyPhotosActivity.this, R.color.theme, "类型", "Name", vm.getTypeList(), new DropDownDialog.SelectedListener() {
                @Override
                public void setSelect(Object o, int position) {
                    vm.getType().setValue(vm.getTypeList().get(position));
                    if (vm.getTypeList().get(position).getName().equals("理货")) {
                        binding.tvSubType.setVisibility(View.VISIBLE);
                    } else {
                        binding.tvSubType.setVisibility(View.GONE);
                    }
                }
            }).show();
        }

        public void selectSubType() {
            new DropDownDialog<>(TallyPhotosActivity.this, R.color.theme, "类型", "PhotoTypeName", vm.getSubList().getValue(), new DropDownDialog.SelectedListener() {
                @Override
                public void setSelect(Object o, int position) {
                    vm.getSubType().setValue(vm.getSubList().getValue().get(position));
                }
            }).show();
        }

    }
}
